package engine.util
{
	import com.greensock.TweenMax;
	
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	/**
	 *常用滤镜效果
	 *  
	 * @author guyi
	 * 
	 */
	public class FilterUtil
	{
		public function FilterUtil()
		{
		}
		
		static private 	var 	_red:Number			= 0.3086; 
		static private 	var		_green:Number		= 0.694; 
		static private  	var 	_blue:Number		= 0.0820; //这三个值是提供标准的黑白效果
		
		
		
		/**
		 *黑白禁用效果滤镜 
		 */		
		static public 		var 	GrayColorFilter : ColorMatrixFilter = new ColorMatrixFilter(new Array(
																						_red, 	_green,	_blue,	0,	0,
																						_red,	_green,	_blue,	0,	0,	
																						_red,	_green,	_blue,	0, 	0, 	
																						0, 		0, 		0, 		1, 	0)
																					);	
		/**
		 *白色发光文字描边 
		 */
		static public 		var 	WhiteColorFilter : GlowFilter = new GlowFilter(0xffffff,0.5);
		
		/**
		 *绿色发光滤镜，用在NPC滑过状态 
		 */		
//		static public var greenGlowFilter	:	GlowFilter	=	new GlowFilter(0x00ff00,1,4,4,5,1);
		
		static public var WhiteGlowFilter:	GlowFilter	=	new GlowFilter(0xffffff, .7, 10, 10 );
		
		/**
		 *模糊滤镜，用在NPC未启用状态 
		 */		
		//		static public var greenGlowFilter	:	GlowFilter	=	new GlowFilter(0x00ff00,1,4,4,5,1);
		
		static public 		var 	GrayColorFilter1 : ColorMatrixFilter = new ColorMatrixFilter(new Array(
																							_red, 	_green,	_blue,	0,	0,
																							_red,	_green,	_blue,	0,	0,	
																							_red,	_green,	_blue,	0, 	0, 	
																							0, 		0, 		0, 		0.3, 	0)
		);
		
		static public function mouseOverOutLab(target:DisplayObject,overColor:String,normalStr:String):void
		{
			var _tip:TipsMainLogic=TipsMainLogic.instance;
			if(target.hasOwnProperty("htmlText"))
			{
				target.addEventListener(MouseEvent.ROLL_OVER,function(e:MouseEvent):void{
					e.currentTarget.htmlText	=	_tip.getUnderLineHtml(
						TipsMainLogic.instance.getColorHtml(e.currentTarget.text,"00ff00")
						,"d");
				});
				target.addEventListener(MouseEvent.ROLL_OUT,function (e:MouseEvent):void{
					e.currentTarget.htmlText	=	_tip.getUnderLineHtml(normalStr,"d");
				});
				target["htmlText"]	=	_tip.getUnderLineHtml(normalStr,"d");
			}
		}
		
		/**
		 *文本的滤镜效果 
		 */		
		static public var txtGlowFilter		:GlowFilter		=	new GlowFilter(0x000000,1,2,2,4,BitmapFilterQuality.HIGH);
		static private var timer:Timer=new Timer(200);
		static private var targetArr:Vector.<Object>= new Vector.<Object>;
		static private var colorTr:ColorTransform=new ColorTransform(1,1,1,1,255,255,-255,0);
		/**
		 *文本变色效果 
		 * @param target
		 * @param args
		 * 
		 */		
		static public function animationText(target:DisplayObject,...args):void
		{
			if(!timer.hasEventListener(TimerEvent.TIMER))
			{
				timer.addEventListener(TimerEvent.TIMER,timerHandle);
			}
			var obj:Object={target:target,times:2,state:0};
			if(args&&args.length)
			{
				for  each(var i:Object in args) 
				{
					for  (var j:String in i) 
					{
						obj.changeName=j;
						obj.changeValue=i[j];
					}
				}
			}
			targetArr.push(obj);
			timer.reset();
			timerHandle(null);
			timer.start();
			function	timerHandle(e:Event):void{
				var temp:Array=[];
				for each (var i:Object in targetArr) 
				{
					var targetIt:DisplayObject=i.target;
					if(i.times>0)
					{
						if(i.state==0)
						{
							targetIt.transform.colorTransform=colorTr;
							i.state=1;
						}
						else
						{
							targetIt.transform.colorTransform=new ColorTransform;
							i.state=0;
							if(i.changeName)
							{
								target[i.changeName]=i.changeValue;
							}
							i.times--;
						}
//						trace("FilterUtil.enclosing_method(e)",targetArr.indexOf(i),i.state,i.times);
					}
					else
					{
						temp.push(i);
					}
				}
				for each ( i in temp) 
				{
					var index:int=targetArr.indexOf(i);
					if(index!=-1)
						targetArr.splice(index,1)
				}
				if(!targetArr.length)
				{
					timer.stop();
				}
				
			}
//			target.transform.colorTransform=colorTr;
//			TweenMax.to(target, 0.5, {colorTransform:{tint:0xff0000, tintAmount:0.5},onComplete :function():void{
//				TweenMax.to(target, 0.5, {colorTransform:{tint:0xff0000, tintAmount:0}})
//			}})
		}

	}
}